package com.kye.express.business.jd.shop.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.jd.shop.model.JDGoodsItemModel;
import com.kye.express.databinding.ItemJdGoodsBinding;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/01/2018
 * @updated : 29/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDGoodsViewBinder extends ItemViewBinder<JDGoodsItemModel, JDGoodsViewBinder.ViewHolder> {

    private OnGoodsItemListener onItemListener;

    public JDGoodsViewBinder(OnGoodsItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_goods, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDGoodsItemModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdGoodsBinding binding;
        private JDGoodsItemModel item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.goodsItem)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onGoodsItemClicked(item);
                            }
                        }
                    });
        }

        public void bindData(JDGoodsItemModel item) {
            this.item = item;
            binding.setData(item);
            binding.dividerLeft.setVisibility(item.isShowLeftDivider() ? View.VISIBLE : View.GONE);
            binding.dividerRight.setVisibility(!item.isShowLeftDivider() ? View.VISIBLE : View.GONE);
            Double score = item.getGoodsItemBean().getNeedScore() * item.getGoodsItemBean().getExchangeRate();
            if (score > 10000) {
                Double newScore = score / 10000;
                binding.score.setText(String.valueOf(newScore.intValue()) + "万积分");
            } else {
                binding.score.setText(String.valueOf(score.intValue()) + "积分");
            }
        }
    }

    public interface OnGoodsItemListener {
        void onGoodsItemClicked(JDGoodsItemModel item);
    }
}
